/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import interfaces.KundeInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jonas
 */
public class Kunde extends UnicastRemoteObject implements KundeInterface {

    private int id;
    private String pw;
    private String name;
    private ResultSet rs;

    public Kunde(int id, String name, String pw) throws RemoteException {
        this.id = id;
        this.pw = pw;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getPw() {
        return pw;
    }

    public String getName() {
        return name;
    }

    public String valueToString() throws RemoteException {
        return getName() + ": " + getPw();
    }

    public String showVarer() {
        String vareliste = "";

        for (Vare vare : Main.vh.getVareList()) {
            int rabat = getRabat(vare.getVareVG());
            vareliste = vareliste + vare.getVareNavn() + "\n" + (vare.getVarePris() - (vare.getVarePris() * rabat / 100)) + " kr. (Rabat: + " + rabat + "%)\tAntal: " + vare.getVareAntal() + "\n\n";
        }
        return vareliste;
    }

    public int getRabat(int vgID) {
        int rabat = 0;
        String sql = "call getRabat(" + getId() + ", " + vgID + ")";
        try {
            rs = Main.dh.getStmt().executeQuery(sql);
            if (rs.next()) {
                rabat = rs.getInt("rabat_sats");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Kunde.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rabat;
    }
}
